package com.ruoyi.project.person.info.controller;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.cms.CmsNotDeleteException;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.cms.content.domain.CmsContent;
import com.ruoyi.project.cms.content.domain.CmsTypeVo;
import com.ruoyi.project.cms.type.domain.CmsType;
import com.ruoyi.project.person.dept.domain.PersonDept;
import com.ruoyi.project.person.dept.domain.PersonDeptVo;
import com.ruoyi.project.person.dept.service.IPersonDeptService;
import com.ruoyi.project.person.info.domain.PersonInfo;
import com.ruoyi.project.person.info.domain.PersonInfoVo;
import com.ruoyi.project.person.info.service.IPersonInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @program:
 * @auther 草莓酸奶~
 * @date 2020-09-17-16:05
 * @description:
 */

@Controller
@RequestMapping("/person/info")
public class InfoController extends BaseController {

    private String prefix = "person/info";

    @Autowired
    private IPersonInfoService personInfoService;

    @Autowired
    private IPersonDeptService personDeptService;

    /**
     * 创建显示内容管理主页的控制器单元方法
     *
     * @return 人事管理主页的地址
     */
    @RequiresPermissions("person:info:view")
    @GetMapping()
    public String content() {
        return prefix + "/info";
    }


    /**
     * 分页查询信息和条件查询信息
     * @param personInfo
     * @return
     */

    @RequiresPermissions("person:info:list")
    @PostMapping("/list")
        @ResponseBody
        public TableDataInfo list(PersonInfo personInfo){
            startPage();
            List<PersonInfoVo> list=  personInfoService.selectPersonInfoList(personInfo);
            return getDataTable(list);
    }



    /**
     * 跳转员工信息添加页面
     * @return 跳转到类型的添加页的地址
     */
    @RequiresPermissions("person:info:add")
    @GetMapping("/add")
    public String personInfoadd(Model model){
        PersonDept personDept = new PersonDept();
        //调用service查询数据
        List<PersonDept> personDepts = personDeptService.selectPersonDeptList(personDept);
        List<PersonDeptVo> personDeptVos = new ArrayList<>();
        for (PersonDept dept : personDepts) {
            PersonDeptVo personDeptVo = new PersonDeptVo();
            BeanUtils.copyBeanProp(personDeptVo, dept);
            personDeptVos.add(personDeptVo);
        }
        model.addAttribute("vos", personDeptVos);
        return prefix + "/add";
    }
    /**
     * 添加员工信息的操作
     * @param personInfo
     * @return
     */
    @RequiresPermissions("person:info:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(PersonInfo personInfo){

//        if (UserConstants.TYPE_CODE_NOT_UNIQUE.equals(cmsTypeService.checkTypeNameUnique(cmsType)))
//        {
//            return error("新增公告'" +cmsType.getTypeName() + "'失败，公告名称已存在");
//        }

        return toAjax(personInfoService.insertPersonInfo(personInfo));
    }



    /**
     * 修改员工时的数据回显操作
     *
     * @param infoId
     * @param modelMap
     * @return 1)根据id到数据库中查询对应的id,并保存在modelMap中
     * 2)跳转到修改的页面,进行数据回显
     */
    @GetMapping("/edit/{infoId}")
    public String edit(@PathVariable("infoId") Long infoId, ModelMap modelMap) {

        //根据传递的id查询info数据,并将对象放到作用域中
        PersonInfo personInfo = personInfoService.selectPersonInfoById(infoId);
        modelMap.addAttribute("personInfo", personInfo);
        //还需将所有的personInfo对象传递到页面中
        //调用service查询数据
        PersonDept personDept = new PersonDept();
        List<PersonDept> personDepts = personDeptService.selectPersonDeptList(personDept);
        List<PersonDeptVo> personDeptVos = new ArrayList<>();
        for (PersonDept dept : personDepts) {
            PersonDeptVo personDeptVo = new PersonDeptVo();
            BeanUtils.copyBeanProp(personDeptVo, dept);
            personDeptVos.add(personDeptVo);
        }
        modelMap.addAttribute("vos", personDeptVos);


        return prefix + "/edit";
    }
    /**
     * @param personInfo
     * @return
     */
    @RequiresPermissions("person:view:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated PersonInfo personInfo) {

        return toAjax( personInfoService.updatePersonInfo(personInfo));
    }

    /**
     * 删除公告的操作
     *
     * @param ids
     * @return
     */
    @RequiresPermissions("person:view:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long[] ids) {

        try {
            int result = personInfoService.removePersonInfoById(ids);
            return toAjax(result);
        } catch (CmsNotDeleteException e) {
            return AjaxResult.error(e.getMessage());
        }
    }


}
